using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Hoshimi
{
    public class LinearRegression
    {
		#region Variables
		
		protected List<Point> mPoints				= null;
        protected float[] mAveragePoint				= new float[2];
        protected float[] mVariancePoint			= new float[2];
        protected float mCovariance					= 0;
		protected float mLinearCoeffOfCorrelation	= 0;

		#endregion

		#region Getters & Setters
		
		public float[] myAveragePoint			{ get { return this.mAveragePoint; } }
        public float[] myVariancePoint			{ get { return this.mVariancePoint; } }
        public float myCovariance				{ get { return this.mCovariance; } }
		public float myLinearCoeffOfCorrelation { get { return this.mLinearCoeffOfCorrelation; } }

		public List<Point> myPoints				
		{ 
			get { return this.mPoints; }
			set { this.mPoints = value; }
		}

		#endregion

		#region Computation
		
		protected void ComputeAveragePoint()
        {
            this.mAveragePoint[0] = 0;
            this.mAveragePoint[1] = 0;

            foreach (Point point in this.mPoints)
            {
                this.mAveragePoint[0] += point.X;
                this.mAveragePoint[1] += point.Y;
            }

			if (this.mPoints.Count > 0)
			{
				this.mAveragePoint[0] /= this.mPoints.Count;
				this.mAveragePoint[1] /= this.mPoints.Count;
			}
        }

        // Il faut que le point moyen soit correct
        public void ComputeVariancePointAndCovariance()
        {
			this.ComputeAveragePoint();

            this.myVariancePoint[0] = 0;
            this.myVariancePoint[1] = 0;

            this.mCovariance        = 0;

            float deltaX;
            float deltaY;

            foreach (Point point in this.mPoints)
            {
                deltaX = point.X - this.myAveragePoint[0];
                this.myVariancePoint[0] += deltaX * deltaX;

                deltaY = point.Y - this.myAveragePoint[1];
                this.myVariancePoint[1] += deltaY * deltaY;

                this.mCovariance += deltaX * deltaY;
            }

			if (this.mPoints.Count > 0)
			{
				this.myVariancePoint[0] /= this.mPoints.Count;
				this.myVariancePoint[1] /= this.mPoints.Count;

				this.mCovariance		/= this.mPoints.Count;

				this.mLinearCoeffOfCorrelation = this.mCovariance / this.mVariancePoint[0];
			}			
        }

		#endregion
        
		#region Ctor
		
		//Ctor
        public LinearRegression(List<Point> points)
        {
            this.mPoints = points;
        }

		public LinearRegression()
        {

        }

		#endregion
    }
}
